/**
 * Parses a JSON response from the given URL
 * @param url
 */
function uniprotFeaturePainter_jsonFeatureParser(url) {
    var params = jQuery.url(url).param();//Ext.urlDecode(url);
    var propCount = 0;
    for (prop in params) {
        propCount++;
        break;
    }
    if (!params || (propCount == 0)) {
        window.location = "../../image";
        return;
    }
    //console.log(params);
    if (params.segment) {
        var segment = params.segment;
        if (params.option != "image") {
            jQuery.ajax({
                url: '../../image',
                data: params,
                success: function(response, callOptions) {
                    console.log(response);
                    try {
                        var myPainter = new Biojs.FeatureViewer({
                            target: "uniprotFeaturePainter-div",
                            json: response,
                            showSlider: true,
                            showPrintButton: true,
                            showFeaturesTooltip: true,
                            highlightFeatures: true,
                            dragSites: true,
                            featureImageWebService: "http://localhost:8080/image"
                        });
                        myPainter.paintFeatures();
                        // We do not want to customize for the test
                        // myPainter.customize("nonOverlapping",true,true);
                        //We do not want listeners for the test but it is also possible to have them
//                        myPainter.addListener( 'onFeatureClick',
//                             function( obj ) {
//                                 var tooltip = obj.featureLabel +
//                                        " (" + obj.featureStart + ", " + obj.featureEnd + "; length " + (obj.featureEnd-obj.featureStart+1) + ")" +
//                                        "<br/>Type: " + obj.featureTypeLabel + " - " + obj.typeCode + " - " + obj.typeCategory +
//                                        "<br/>Evidence: " + obj.evidenceText + " - " + obj.evidenceCode;
//                                 alert("Clicked: " + tooltip );
//                                 //console.log(obj.shapeOn);
//                             }
//                        );
                    } catch (err) {
                        console.log(err);
                        document.getElementById('uniprotFeaturePainter-div').innerHTML = '';
                        document.getElementById('uniprotFeaturePainter-div').innerHTML = 'No image available. Did you provide a valid UniProt accession or identifier, and valid limits?';
                    }
                },
                error: function(response, callOptions) {
                    console.log(response);
                    document.getElementById('uniprotFeaturePainter-div').innerHTML = '';
                    document.getElementById('uniprotFeaturePainter-div').innerHTML = 'No image available. Did you provide a valid UniProt accession or identifier, and valid limits?';
                }
            });
        } else {
            try {
                //alert(location.pathname);
                //alert(location.search);
                window.location = "../../image" + location.search;
            } catch(err) {
                console.log(err);
                document.getElementById('uniprotFeaturePainter-div').innerHTML = '';
                document.getElementById('uniprotFeaturePainter-div').innerHTML = 'No image available. Did you provide a valid UniProt accession or identifier, and valid limits?';
            }
        }
    } else {
        window.location = "image";
    }
}